/**
 * Copyright (c) 2012-2014 All Rights Reserved.
 */
package com.easy.framwork.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.easy.framwork.jdbc.handlers.ResultSetHandler;
import com.easy.framwork.utils.StringUtils;

/**
 * 
 * @author wy
 * @version v 0.1 2014-1-2 下午8:23:18 wy Exp $
 */
public class QueryHelper extends AbstractQueryHelper {

    /**
     * 更新
     * 
     * @param conn
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    public int update(Connection conn, String sql, Object... params) throws SQLException {

        JdbcAssertUtils.validConnection(conn);

        if (StringUtils.isBlank(sql)) {
            throw new SQLException("Blank SQL statement");
        }

        PreparedStatement stmt = null;

        long currentTime = System.currentTimeMillis();
        try {
            stmt = this.prepareStatement(conn, sql);

            if (params != null) {
                this.fillStatement(stmt, params);
            }
            return stmt.executeUpdate();

        }
        catch (SQLException e) {
            this.throwSQLException(e, sql, (Object[]) params);
        }
        finally {
            logSQL(System.currentTimeMillis() - currentTime, sql, params);
        }
        return 0;
    }

    /**
     * 批量操作
     * 
     * @param conn
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    public int[] batch(Connection conn, String sql, Object[] params) throws SQLException {

        JdbcAssertUtils.validConnection(conn);

        if (StringUtils.isBlank(sql)) {
            throw new SQLException("Blank SQL statement");
        }

        if (params == null) {
            throw new SQLException("Null parameters");
        }

        PreparedStatement stmt = null;
        int[] rows = null;

        long currentTime = System.currentTimeMillis();
        try {
            stmt = this.prepareStatement(conn, sql);
            for (int i = 0; i < params.length; i++) {
                this.fillStatement(stmt, params[i]);
                stmt.addBatch();
            }
            rows = stmt.executeBatch();

        }
        catch (SQLException e) {
            this.throwSQLException(e, sql, (Object[]) params);
        }
        finally {
            logSQL(System.currentTimeMillis() - currentTime, sql, params);
        }
        return rows;
    }

    /**
     * 查询
     * 
     * @param conn
     * @param sql
     * @param rsh
     * @param params
     * @return
     * @throws SQLException
     */
    public <T> T query(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params)
                                                                                              throws SQLException {

        JdbcAssertUtils.validConnection(conn);

        if (StringUtils.isBlank(sql)) {
            throw new SQLException("Blank SQL statement");
        }

        PreparedStatement stmt = null;

        ResultSet rs = null;
        T result = null;

        long currentTime = System.currentTimeMillis();
        try {
            stmt = this.prepareStatement(conn, sql);
            this.fillStatement(stmt, params);
            rs = stmt.executeQuery();
            result = rsh.handle(rs);
        }
        catch (SQLException e) {
            this.throwSQLException(e, sql, (Object[]) params);
        }
        finally {
            logSQL(System.currentTimeMillis() - currentTime, sql, params);
        }

        return result;
    }

}
